package easyjava.hibernate.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import easyjava.hibernate.bean.CourseTemplate;


public class CourseTemplateDao {
	private Session session;
	public CourseTemplateDao(Session session ){
		this.session=session;
	}
	
	public List<CourseTemplate> list(){
		Query<CourseTemplate> query =session.createQuery("from CourseTemplate",CourseTemplate.class);
		return query.getResultList();
	}
	public CourseTemplate getCourseTemplateById(int id){
		return session.get(CourseTemplate.class, id);
	}
	
	public void insert(CourseTemplate courseTemplate) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(courseTemplate);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(CourseTemplate courseTemplate){
		Transaction tx = session.beginTransaction();
		try {

			session.update(courseTemplate);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
	public void delete(CourseTemplate courseTemplate){
		Transaction tx = session.beginTransaction();
		try {
			
			session.delete(courseTemplate);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}

}
